/// <reference path="./jquery.d.ts" />


// Interface
interface IUnitParser {
    parse(html:string): IUnit[] ;
}

interface IUnit
{
    id: number;
    armor: number;
    experience: number;
}

interface IMap
{
    tiles: ITile[][];
}

interface ITile
{
    row: number;
    column: number;
}

// Module
module BattleThis
{

    export class ArgException implements Error
    {
        public name = "ArgException";

        constructor(public argName?: string, public message?: string) { }

    }

    export class ParserException implements Error
    {
        public name = "ParserException";
        constructor( public message: string, public innerEx:Error) { }

    } 

    export class Unit implements IUnit
    {

        constructor( public id: number, public armor: number, public experience: number)
        {
            if (id < 0 || id.toString() == "") throw new ArgException("id");
            if (armor <= 0) throw new ArgException("id");
            if (experience < 0) throw new ArgException("experience");

            this.id = id;
            this.armor = armor;
        }

    }

    export class Map implements IMap
    {
        constructor(public tiles: ITile[][])
        {
            if (tiles === null || tiles === undefined)
                throw new ArgException("tiles", "Is Null.");
        }

    }

    export class Tile implements ITile
    {
        constructor(public column: number, public row: number)
        {
            if (column < 0)
                throw new ArgException("column", "Is < 0");

            if (row < 0)
                throw new ArgException("row", "Is < 0");
        }
    }

    export class UnitParser implements IUnitParser {

        /**
        * Parse all Units from the given html-Fragment.
        * @param Html which will be parsed.
        */
        parse(html: string): IUnit[] {

            var units = new Unit[];
            var fragments = $(html).filter('.InfoUnit').toArray();
            fragments.forEach(
                function (value, index) {
                    try {
                        var query = $(value);
                        var id = <number> query.find('[name]*="Id"').val();
                        var armor = <number> parseInt(query.find('.damage span').text());
                        var experience = <number> parseInt(query.find('.expierence img').attr('title'));
                    }
                    catch (ex) {
                        throw new ParserException("Error by parsing this value:'" + value + "'", ex);
                    }

                    units[index] = new Unit(id, armor, experience);

                });

            return units;
        }
    }
    
    export class MapParser
    {
        parse(html:string) : IMap {

            if (html.length <= 0)
                throw new ArgException("html", "Is empty.");

            var tiles = new Tile[][];

            var fragments = $(html).find('div .gameField').toArray();
            fragments.forEach(
                function (value, index) {
                    try {
                        var query = $(value);
                        var idToParse = query.attr("id").split("_");
                        var column =parseInt( idToParse[0]);
                        var row = parseInt(idToParse[1]);
                    }
                    catch (ex) {
                        throw new ParserException("Error by parsing this value:'" + value + "'", ex);
                    }

                    var rows = tiles[column];
                    if (rows === null || rows === undefined)
                    {
                        tiles[column] = new Tile[];
                    }

                    tiles[column][row] = new Tile(column, row);

                });


            return new Map(tiles);
        };

    }
}
